package com.application.moneycalculator.utils;

import java.util.Calendar;

import com.application.moneycalculator.data.ExpenseItem;

public class DateUtils {
	
	private static final String TAG = "Utils";
	
	private static String separator = "-";
	private static String point = ".";
	
	public static Calendar getCurrentDate()
	{
		Calendar c = Calendar.getInstance(); 
		return c;
		
	}
	
	public static String convertExpenseToString(ExpenseItem expense)
	{
		Calendar c = expense.getDate();
		
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH) + 1;
		int date = c.get(Calendar.DATE);
		
		int hour = c.get(Calendar.HOUR);
		int minute = c.get(Calendar.MINUTE);
		int second = c.get(Calendar.SECOND);
		
		String result = "" + year + separator + month + separator + date
						+ point + 
						hour + separator + minute + separator + second;
		
		return result;
	}
	
	public static ExpenseItem convertStringToExpense(String str)
	{
		Calendar c = Calendar.getInstance();
		
		int posStartYear = 0; 
		int posEndYear = str.indexOf(separator, posStartYear);
		int year = Integer.parseInt(str.substring(posStartYear, posEndYear));
		
		int posStartMonth = posEndYear + 1; 
		int posEndMonth = str.indexOf(separator, posStartMonth);
		int month = Integer.parseInt(str.substring(posStartMonth, posEndMonth)) - 1;
		
		int posStartDate = posEndMonth + 1; 
		int posEndDate = str.indexOf(point, posStartDate);
		int date = Integer.parseInt(str.substring(posStartDate, posEndDate));
		
		/*************************************************/
		
		int posStartHour = posEndDate + 1; 
		int posEndHour = str.indexOf(separator, posStartHour);
		int hour = Integer.parseInt(str.substring(posStartHour, posEndHour));
		
		int posStartMinute = posEndHour + 1; 
		int posEndMinute = str.indexOf(separator, posStartMinute);
		int minute = Integer.parseInt(str.substring(posStartMinute, posEndMinute));
		
		int posStartSecond = posEndMinute + 1; 
		int posEndSecond = str.length();
		int second = Integer.parseInt(str.substring(posStartSecond, posEndSecond));
		
		
		c.set(year, month, date, hour, minute, second);
		
		ExpenseItem expense = new ExpenseItem(c);

		return expense;
	}
	
	
}
